package pATT.bNEdit.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import pATT.bNEdit.base.Nodo;
import pATT.bNEdit.base.RedBayes;



/**
 * <p>Title: BNEdit</p>
 * <p>Description: Bayesian Network Editor</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Luciana Zavaleta
 * @version 1.0
 */

@SuppressWarnings("unused")
public class ExperiencePanel extends JPanel implements Actualizable{

  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private JTable tableExp = new JTable();
  private RedBayes bayesianNetwork = null;
  private String nodeName =null;
private BNEditInternalFrame internalFrame = null;


  JLabel jLabel1 = new JLabel();
  JLabel jLabel3=new JLabel();
  JScrollPane jScrollPane1 = new JScrollPane();
  Border border1;
  TitledBorder titledBorder1;
  JLabel jLabel2 = new JLabel();
  JLabel labelParents = new JLabel();



  public ExperiencePanel() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
    Component[] components = this.getComponents();
    for (int i = 0; i < components.length; i++) {
      components[i].setEnabled(true);
    }

  }

  //Component initialization
  void jbInit() throws Exception {

    border1 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(165, 163, 151));
    titledBorder1 = new TitledBorder(border1,"Experience Table");
    jLabel1.setText("Variable name:");
    jLabel3.setMinimumSize(new Dimension(65, 15));
    jLabel3.setPreferredSize(new Dimension(65, 15));
    jLabel3.setText(nodeName);
    jLabel2.setText("Parent node names:");
    this.setLayout(new FlowLayout());

    jScrollPane1.setBorder(BorderFactory.createLineBorder(Color.black));
    jScrollPane1.setPreferredSize(new Dimension(210, 100));
    this.setBorder(titledBorder1);
    this.setPreferredSize(new Dimension(230, 400));
    this.add(jLabel1, null);
    this.add(jLabel3,null);
    this.add(jLabel2,null);
    this.add(labelParents, null);
    this.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(tableExp, null);

    tableExp.setAutoscrolls(true);
    tableExp.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

  }

  /**
   * setBayesianNetwork
   * @param bayesianNetwork RedBayes
   */
  public void setBayesianNetwork(RedBayes bayesianNetwork) {
    this.bayesianNetwork = bayesianNetwork;
    setCurrentNode(null);
  }

  /**
   * Sets the currently selected node name
   * @param nodeName String
   */

  public void setCurrentNode(String nodeName) {
    this.nodeName = nodeName;
    Component[] components = this.getComponents();
    Nodo node=null;
    if (nodeName!=null)
      node= bayesianNetwork.getNodo(nodeName);
    if (nodeName==null || node==null || !node.hasExperienceTable()) {
      nodeName=null;
      labelParents.setText("");
      for (int i = 0; i < components.length; i++) {
        components[i].setVisible(false);
      }
    }
    else {

      tableExp.setModel(new ExperienceTableModel(node));
      for (int i = 0; i < components.length; i++) {
        components[i].setVisible(true);
      }

      jLabel3.setText(nodeName);
      Enumeration parentsEnum =
          bayesianNetwork.getNodo(nodeName).getPadres().elements();
      StringBuffer parentNames = new StringBuffer();
      if (parentsEnum.hasMoreElements()) {
        String parentName =
            bayesianNetwork.NombreExt((Nodo)parentsEnum.nextElement());
        parentNames.append(parentName);
      }
      while (parentsEnum.hasMoreElements()) {
        String parentName =
            bayesianNetwork.NombreExt((Nodo)parentsEnum.nextElement());
        parentNames.append(", ");
        parentNames.append(parentName);
      }
      labelParents.setText(parentNames.toString());

    }
  }


  /*
   * Sets the BNEditInternalFrame
   * @param internalFrame BNEditInternalFrame
   */
  public void setInternalFrame(BNEditInternalFrame internalFrame) {
    this.internalFrame = internalFrame;
  }

  /**
   * Updates the node's probabilities
   * @param nodeName String - the node's name
   */
  public void update (String nodeName){
    this.setCurrentNode( nodeName);
    ((ExperienceTableModel)tableExp.getModel()).update(bayesianNetwork);
  }

}